﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DCT.Service.Restriction.Utility
{
    /// <summary>
    /// 集合工具类
    /// </summary>
    public static class CollectionUtility
    {
        /// <summary>
        /// 集合为空或者null
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static bool IsEmptyOrNull(ICollection collection)
        {
            return collection == null || collection.Count == 0;
        }

        /// <summary>
        /// 把IEnumerable转化为Dictionary
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static Dictionary<TKey, TValue> Convert<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> collection)
        {
            if (collection == null) {
                return null;
            }

            Dictionary<TKey, TValue> dict = new Dictionary<TKey, TValue>();
            foreach (var item in collection)
            {
                dict[item.Key] = item.Value;
            }
            return dict;
        }
    }
}
